package login;

import io.ClientSession;
import io.OperationResponse;

import java.io.IOException;
import java.util.HashMap;

import org.apache.http.client.ClientProtocolException;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import platform.AppConfig;
import protocol.ParameterType;
import protocol.ReturnCode;

/**
 * 
 * 
 * @author zhengjiangyu
 * 
 */
public class XYlogin {
	protected static final Logger log = LoggerFactory.getLogger(XYlogin.class);

	public static String url = "http://passport.xyzs.com/checkLogin.php";
	static String appid="100002364";
	public static void Login(OperationResponse operationRequest,
			ClientSession session) throws ClientProtocolException, IOException, JSONException {
		if (operationRequest.Parameters.size() < 2) {
			OperationResponse response = new OperationResponse(
					operationRequest.OperationCode, null, ReturnCode.No, "");
			session.SendOperationResponse(response);
		}
		String uid = (String) operationRequest.Parameters
				.get(ParameterType.UserID);
		String sessionid = (String) operationRequest.Parameters
				.get(ParameterType.UserPwd);
		String deviceID = (String) operationRequest.Parameters
				.get(ParameterType.DeviceID);
		String isGetIP = (String) operationRequest.Parameters
				.get(ParameterType.IsGetIP);
		
		String PhoneType=(String) operationRequest.Parameters
				.get(ParameterType.PhoneType);
		if (uid.equals("zealm")&&sessionid.equals("")) {
			OperationResponse response = new OperationResponse(
					operationRequest.OperationCode, AppConfig.dicAppInfo,
					ReturnCode.GetServer, "");
			session.SendOperationResponse(response);
			return;

		}
		String token="";
		String par[]=uid.split("[;]", -1);
		uid =par[1];
		token=par[0];
		
		String body="uid="+uid+"&"+"appid="+appid+"&"+"token="+token;
		body = new String(body.getBytes("gbk"), "utf-8");
//		HttpsUtil.post(serverUrl, body);
		String result =Util.sendPost(url, body);
		if(!result.equals("")){
			JSONObject date=new JSONObject(result);
			String status=date.optString("ret");
			if(status.equals("0")){
				uid="XY"+uid;
				operationRequest.Parameters.put(ParameterType.UserID, uid);
				HashMap<Short, Object> myPrams = new HashMap<Short, Object>();
				myPrams.put(ParameterType.UserID, uid);
				OperationResponse responseID = new OperationResponse(
						operationRequest.OperationCode, myPrams,
						ReturnCode.HasID, "");
				session.SendOperationResponse(responseID);
				Util.sdkReceived(operationRequest, session,uid, deviceID, isGetIP,"XY",PhoneType,"");
			}else{
				return;
			}
		}else {
			log.info("XY return have a error result ="+result);
		}
		

	}
	public  Runnable getLoginXY(OperationResponse operationResponse, ClientSession session){ 
		class LoginXY implements Runnable { 
			private OperationResponse t_operationResponse;
			private ClientSession t_session;
			public LoginXY(OperationResponse operationResponse, ClientSession session)
			{
				t_operationResponse = operationResponse;
				t_session = session;
			}
			
			@Override
			public void run() {
					try {
						Login(t_operationResponse, t_session);
					} catch (Exception e) {
						log.info(Thread.currentThread().getName() + " Login task Error!!!");
					}
				}
        }
	    return new LoginXY(operationResponse, session); 
	 } 

}
